package com.oscard.xml;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Dictionary;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;


public class saxXML {
	private URL rssUrl;
 

	public saxXML(String url ) {
		try {
			
			
			this.rssUrl = new URL(url);
			
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	public ArrayList<Dictionary<String, String>> parse(int type) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			if(type == 1){
				SAXParser parser = factory.newSAXParser();
				handXML handler = new handXML();
				parser.parse(this.getInputStream(), handler);
				return handler.getXML();
			}else if(type == 2){
				SAXParser parser = factory.newSAXParser();
				handXMLCommand handler = new handXMLCommand();
				parser.parse(this.getInputStream(), handler);
				return handler.getXML();			
			}else{
				return null;
			}
		} catch (Exception e) {
			 return null;
		}
	}

	private InputStream getInputStream() {
		try {
//			clsErrorInternet.setErrorInternet(false);
			URLConnection connection = rssUrl.openConnection();
			connection.setConnectTimeout(10000);
			return connection.getInputStream();

		} catch (IOException e) {
//			clsErrorInternet.setErrorInternet(true);
			
			 e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
